﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using CoiffeurProject.Screens;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using CoiffeurProject.Utils;
using System.Windows.Media;

namespace CoiffeurProject.UserControls
{
    internal class LabeledButton
    {
        Grid main_container;
        Label label;
        Image active, not_active;
        SolidColorBrush active_color, unactive_color;
        Button button;
        bool isActive;

        internal LabeledButton(Thickness margin, String active_image_uri, String unactive_image_uri,
            String label, int row, int col, int width, int height, bool isActive)
        {
            this.isActive = isActive;

            not_active = new Image() { Source = new BitmapImage(new Uri(unactive_image_uri)), Stretch = Stretch.Fill };
            active = new Image() { Source = new BitmapImage(new Uri(active_image_uri)), Stretch = Stretch.Fill };

            active_color = new SolidColorBrush(Colors.White);
            unactive_color = new SolidColorBrush(Colors.LightGray);

            main_container = new Grid()
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Width = width,
                Height = height,
                Margin = margin
            };

            Grid.SetColumn(main_container, col);
            Grid.SetRow(main_container, row);
            
            button = new Button()
            {
                Width = width,
                Height = height,
                Content = isActive ? active : not_active
            };

            button.SetResourceReference(Button.StyleProperty, "NoChromeButton");            

            main_container.Children.Add(button);

            this.label = new Label()
            {
                Content = label,
                Width = width,
                Height = height,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                FontFamily = new FontFamily("Segoe UI"),
                FontSize = 22,
                Foreground = isActive ? active_color : unactive_color,
                IsHitTestVisible = false
            };

            main_container.Children.Add(this.label);
        }

        public void AddClickEventHandler(Action<object, RoutedEventArgs> action)
        {
            button.Click += new RoutedEventHandler(action);
        }

        public void ToggleActive()
        {
            isActive = !isActive;
            button.Content = isActive ? active : not_active;
            label.Foreground = isActive ? active_color : unactive_color;
        }

        public void MakeActive()
        {
            isActive = true;
            button.Content = active;
            label.Foreground = active_color;
        }

        public void MakeUnactive()
        {
            isActive = false;
            button.Content = not_active;
            label.Foreground = unactive_color;
        }

        public bool IsActive
        {
            get { return isActive; }
        }

        public Grid MainContainer
        {
            get { return main_container; }
        }
    }
}
